package com.yjps.system.controller;

import com.alibaba.fastjson.JSON;
import com.yjps.common.util.CommonUtil;
import com.yjps.system.model.Agent;
import com.yjps.system.model.Shop;
import com.yjps.system.model.User;
import com.yjps.system.service.AgentService;
import com.yjps.system.service.ShopService;
import com.yjps.system.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by jessy on 2015/8/19.
 */
@Controller
@RequestMapping("/system/agent")
public class AgentController {

    Logger log = LoggerFactory.getLogger(AgentController.class);
    @Autowired
    AgentService agentService;
    @Autowired
    UserService userService;
    @Autowired
    ShopService shopService;

    @ResponseBody
    @RequestMapping(value = "/agentInfo", method = RequestMethod.GET)
    public String agentInfo(@RequestParam(value = "user_id", required = true) String user_id) {
           /*Agent agent = agentService.findAgentByUserId(Integer.parseInt(user_id));
           String jsonStr = JSON.toJSONString(agent);*/
        return null;
    }

    @RequestMapping(value = "/agentList", method = RequestMethod.GET)
    public String agentList(HttpServletRequest request,/*@RequestParam(value = "user_id",required = true)String user_id,*/Model model) {
        /*门店经理*/
        User user = (User) request.getSession().getAttribute("loginUser");
        List<Agent> agents = agentService.findAgentsByUserId(user.getId());
        for(Agent agent : agents){
            /*查找出门店经纪人对应的用户的信息*/
            User userTemp = userService.findUserAgentId(agent.getId());
            agent.setAgentName(userTemp.getRealName());
        }
        model.addAttribute("agentList", agents);
        return "/system/manager/editAgent";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam(value = "id", required = true) String id) {
        agentService.delete(Integer.parseInt(id));
        return "redirect:/system/agent/agentList";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addForm(@RequestParam(value = "id", required = true) String id, Model model) {
        User user = userService.findUser(Integer.parseInt(id));
        model.addAttribute("user", user);
        return "/system/manager/addAgent";
    }
/*修改isValid为int类型  baseDao invalid方法为 isValid
* 把shop添加 shopManagerId字段
* */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestParam(value = "user_id", required = true) String userId, Agent agent, HttpServletRequest request) {
        //agent 该经纪人的门店经理是正在操作的门店经理

        //得到正在操作/正在添加门店经纪人的门店经理
        User shopManager = (User) request.getSession().getAttribute("loginUser");
        agent.setBuildingId(0);
        agent.setUserId(shopManager.getId());
        agent.setJoinTime(CommonUtil.dataToString(new Date()));
        //查找出门店经理所在的门店id
        Shop shop = shopService.findShopByShopManagerId(shopManager.getId());
        if (shop != null)
            agent.setShopId(shop.getId());
        agent.setIsValid(1);
        agentService.save(agent);
        log.debug("save agent info successfully ");
        //userId对应得用户 添加成经纪人
        User user = userService.findUser(Integer.parseInt(userId));
        user.setAgentsId(agent.getId());
        userService.updateUser(user);
        log.debug("agent's userInfo save successfully ");
        return "redirect:/system/agent/agentList";
    }
    @ResponseBody
    @RequestMapping(value = "/getAgent",method = RequestMethod.GET)
    public String getAgent(@RequestParam(value = "id", required = true) String id){
        User user = userService.findUserAgentId(Integer.parseInt(id));
        Agent agent = agentService.findAgentById(Integer.parseInt(id));
        agent.setAgentName(user.getRealName());
        String jsonStr = JSON.toJSONString(agent);
        System.out.println("agent json str = " + jsonStr);
        return jsonStr;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Agent agent){
        agentService.update(agent);
        return "redirect:/system/agent/agentList";
    }
}
